"""
首页视图 - 门户网站
"""

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from pecco_site.models import (
    HomeLayoutBlock, CarouselItem, Category, Product
)
from ..serializers import (
    CarouselSerializer, CategorySerializer, ProductListSerializer
)


class HomePageView(APIView):
    """
    首页数据 API
    
    GET /api/shop/home/?locale=en
    """
    permission_classes = []  # 公开访问
    
    def get(self, request):
        locale = request.GET.get('locale', 'en')
        
        # 获取首页布局块
        layout_blocks = HomeLayoutBlock.objects.filter(
            is_active=True
        ).order_by('sort_order')
        
        blocks = []
        
        for block in layout_blocks:
            block_data = {
                'type': block.block_type,
                'sort_order': block.sort_order,
            }
            
            # 根据块类型获取对应数据
            if block.block_type == 'carousel':
                # 轮播图
                carousels = []
                carousel_items = CarouselItem.objects.filter(
                    is_active=True
                ).order_by('sort_order')
                
                for item in carousel_items:
                    # 获取对应语言的翻译
                    translation = item.translations.filter(locale=locale).first()
                    if translation:
                        carousel_data = {
                            'id': item.id,
                            'image_url': request.build_absolute_uri(item.image.url) if item.image else None,
                            'title': translation.title,
                            'subtitle': translation.subtitle,
                            'cta_text': translation.cta_text,
                            'link': item.link or '',
                        }
                        carousels.append(carousel_data)
                
                block_data['items'] = carousels
            
            elif block.block_type == 'categories':
                # 分类
                categories = []
                category_items = Category.objects.filter(
                    is_active=True
                ).order_by('sort_order')
                
                for cat in category_items:
                    translation = cat.translations.filter(locale=locale).first()
                    if translation:
                        category_data = {
                            'id': cat.id,
                            'slug': cat.slug,
                            'name': translation.name,
                            'icon_url': request.build_absolute_uri(cat.icon.url) if cat.icon else None,
                        }
                        categories.append(category_data)
                
                block_data['items'] = categories
            
            elif block.block_type == 'products':
                # 精选产品
                products = []
                product_items = Product.objects.filter(
                    is_active=True,
                    tag_featured=True  # 使用 tag_featured 而不是 is_featured
                ).order_by('-created_at')[:8]

                for prod in product_items:
                    translation = prod.translations.filter(locale=locale).first()
                    if translation:
                        # 获取封面图
                        cover_url = request.build_absolute_uri(prod.cover_image.url) if prod.cover_image else None

                        # 获取悬停图
                        hover_url = None
                        if prod.hover_image:
                            hover_url = request.build_absolute_uri(prod.hover_image.url)

                        # 获取价格范围
                        price_range = prod.get_price_range()
                        price_display = price_range['display'] if price_range else 'N/A'

                        product_data = {
                            'id': prod.id,
                            'name': translation.name,
                            'short_desc': translation.short_desc or '',
                            'cover': cover_url,
                            'hover': hover_url,
                            'price': price_display,
                            'min_price': str(price_range['min']) if price_range else '0',
                            'max_price': str(price_range['max']) if price_range else '0',
                            'tags': {
                                'featured': prod.tag_featured,
                                'hot': prod.tag_hot,
                                'new': prod.tag_new,
                            },
                        }
                        products.append(product_data)

                block_data['items'] = products
            
            elif block.block_type == 'about_us':
                # 关于我们
                translation = block.translations.filter(locale=locale).first()
                if translation:
                    block_data['title'] = translation.title
                    block_data['content'] = translation.content
            
            elif block.block_type == 'reseller':
                # 经销商招募
                translation = block.translations.filter(locale=locale).first()
                if translation:
                    block_data['title'] = translation.title
                    block_data['content'] = translation.content
            
            blocks.append(block_data)
        
        return Response({
            'blocks': blocks
        })

